package com.zhangyj.action;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhangyj.util.Tools;

public class ActionServlet extends ActionBaseServlet {

	private static final long serialVersionUID = -8078375339410561893L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestContext requestContext = new RequestContext(request);
		ResponseContext responseContext = new ResponseContext(response);
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0
		response.setDateHeader("Expires", 0); // prevents caching at the proxy
												// server
		try {
			AbstractAction action = context.getAction(requestContext.getAction());
			action.request = requestContext;
			action.response = responseContext;
			String returnPath = null;
			String mn = requestContext.getParameter("action");//Tools.getParameter(requestContext, "action", null);
			if (mn == null) {
				mn = action.getClass().getAnnotation(RequestAction.class).defaultAction();
			}
			Method[] ms = action.getClass().getMethods();
			Class<?> rc = null;
			Object r = null;
			RequestMethod ma = null;
			for (int i = 0; i < ms.length; i++) {
				Method m = ms[i];
				RequestMethod ra = m.getAnnotation(RequestMethod.class);
				if (ra != null && ra.name().equals(mn)) {
					Object[] params = null;
					Class<?>[] cs = m.getParameterTypes();
					if (cs != null) {
						params = new Object[cs.length];
						Annotation[][] as = m.getParameterAnnotations();
						
					}
					ma = ra;
					r = m.invoke(action,params);
					rc = m.getReturnType();
					break;
				}
			}
			if (rc == null || r == null) {
				return;
			}
			if (ma != null) {
				String result = null;
				if (rc.equals(String.class)) {
					result = (String) r;
				} else {
					result = Tools.object2jsonStr(r);
				}
				switch (ma.result()) {
				case JSON:
					break;
				case PATH:
					this.getServletContext().getRequestDispatcher(context.getPage(result)).forward(requestContext, responseContext);
					break;
				case XML:
					break;
				case HTML:
					break;
				}
			}

			if (action.validate()) {
				returnPath = (String) action.doAction();
			} else {
				returnPath = (String) action.validateFailed;
			}

			if (returnPath != null && returnPath.trim().length() > 0) {
				this.getServletContext().getRequestDispatcher(context.getPage(returnPath)).forward(requestContext, responseContext);
			}
		} catch (Exception e) {

			throw new ServletException(e);
		}
	}

	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		this.context.destroy();
		this.context = null;
	}
	public static void main(String[] args) {
		Package p = Package.getPackage("com.zhangyj.impl.action.ClientAction");
		System.out.println(p);
	}
}
